import processing.video.*;
import java.util.Random;
Capture cam;

PImage display;

PImage bug;
PImage magic;

color track;


int level = 1;
int ballRadius = 15;

int blockNum = 30;
int []circleX = new int[3000];
int []circleY = new int[3000];
int []directionX = new int[3000];
int []directionY = new int[3000];
double []bugsize = new double[3000];
int movSpeed  = 2;

int magicX;
int magicY;
int startX;
int startY;
int endX;
int endY;
boolean settrack = false;
boolean started = false;
PFont fontA ;
boolean magicUsed = false;


// Set the font and its size (in units of pixels)
void reset(int n,boolean t){
  blockNum = n;
  settrack = t;
  size(1600, 1200);
  
  magicUsed = false;
  
  magicX = (int)random(0,width);
  magicY = (int)random(0,height);
  startX = (int)random(0, width);
  startY = (int)random(0, height);
  endX = (int)random(0, width);
  endY = (int)random(0, height);
  
  for(int i=0;i<blockNum;i++){
    directionX[i] = (int)random(0,6) -3;
    directionY[i] = (int)random(0,6) -3;
    circleX[i] = (int)random(0, width);
    circleY[i] = (int)random(0, height);
    bugsize[i] = random(1,3);
  }
}

void setup() {
  bug = loadImage("Nuvola_apps_bug.png");
  magic = loadImage("magic-1.png");

  
  fontA = loadFont("Monospaced-48.vlw");
  
  textFont(fontA, 36);

  reset(blockNum,false);
  cam = new Capture(this, width, height);
  //cam.start();
  
  track = color(0);
  
  display = new PImage(width, height);
}

void draw() {
  
  int cx = -100; 
  int cy = -100;
  
  if (cam.available()) {
    cam.read();
    cam.loadPixels();

    int[] pixels = cam.pixels;

    float closest = 10000;

    for (int x = 0; x < cam.width; x++) {
      for (int y = 0; y < cam.height; y++) {
        int i = x + cam.width * y;
        int ri = (width - x - 1) + cam.width * y;
        
        display.pixels[ri] = cam.pixels[i];
        
        float colorDist = dist(
          red(cam.pixels[i]), green(cam.pixels[i]), blue(cam.pixels[i]), 
          red(track), green(track), blue(track));
        
        if(colorDist < closest){
          closest = colorDist;
          cx = width - x - 1;
          cy = y;
        }
      }
    }
    display.updatePixels();
    image(display, 0, 0);
 
  }
  
 fill(0, 0, 0);
 ellipse(startX, startY, 50, 50);
 fill(255,255, 255);
 ellipse(endX, endY, 50, 50);
 
 textFont(fontA, 10);
 text("LV: "+level, width-100, 10);
  text("bug_speed: "+movSpeed, width-100, 20);
  text("bug_amount: "+blockNum,width-100,30);

  
  if(settrack ==true){
    if(started == true){
        for(int i = 0;i < blockNum;i++){
          if(dist(circleX[i]+20, circleY[i]+20, cx, cy) < 20*bugsize[i]){
             stop();
             //you lost
             text("You lose, man!", 0, height-10);

              break;
          } else {
            image(bug, circleX[i], circleY[i],(int)(40*bugsize[i]),(int)(40*bugsize[i]));
            
          }
        }
        
       if(magicUsed == false){
         image(magic,magicX,magicY,50,50);
       
         if(dist(magicX,magicY,cx,cy) < ballRadius){
              //next level
              if(blockNum>=5)
                blockNum-=5;
              magicUsed = true;
         }
       }
       
       if(dist(endX,endY,cx,cy) < ballRadius){
            //next level
            level++;
            reset(blockNum+=5,true);
            resume();
       }
       
       
        drawbug();
        display.updatePixels();
     }else{
       text("Move the pointer to start point, then game will start!", 0, height-10);
       if(dist(startX,startY,cx,cy) < ballRadius){
         started = true;
         movSpeed = 1;
       }
    }
 }else{
     textFont(fontA, 36);
     text("Please set your track point!", 0, height-10);
 }
 

 fill(track);
 ellipse(cx, cy, 20, 20);
}

void mousePressed() {
  track = display.pixels[mouseX + cam.width * mouseY];
  settrack = true;
  
}
void stop(){
  started = false;
  movSpeed = 0;
}
void resume(){
  started = true;
  movSpeed = 1;
}
void keyPressed(){
 int keyIndex = -1;
 if(key == '1'){
     reset(blockNum,true);
 }
 if(key == '2'){
     stop();
 }
  if(key == '3'){
     resume();
 }
 if(key == '4'){
   movSpeed++;
 }
 if(key == '5'){
   movSpeed--;
 }
}

void drawbug(){
    
 for(int i=0;i<blockNum;i++){
   circleX[i]+=movSpeed*directionX[i];
   circleY[i]+=movSpeed*directionY[i];
   if(circleX[i]<=0){
     circleX[i] = 0;
     directionX[i] = -directionX[i];
   }
   if(circleY[i]<=0){
     circleY[i] = 0;
     directionY[i] = -directionY[i];
   }
   if(circleX[i]>=width-1){
     circleX[i] = width-1;
     directionX[i] = -directionX[i];
   }
   if(circleY[i]>=height-1){
     circleY[i] = height-1;
     directionY[i] = -directionY[i];
   }
  } 
}
